//
//  LoginController.swift
//  SwiftBase
//
//  Created by 郭艳波 on 2018/3/7.
//  Copyright © 2018年 郭艳波. All rights reserved.
//

import UIKit

class LoginController: UIViewController {

    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var panelView: ArcView!
    @IBOutlet weak var collapseBtn: UIButton!
    
    var isCollapsed: Bool!
    enum collapseBtnTitle: String{
        case Up = "收起", Expand = "展开"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 屏幕适配
        setAdaptUI()
        // 初始化页面数据
        initUIData()
        // 设置代理
        setTFDelegate()
        
        self.usernameTF.text = "17681616116"
        self.passwordTF.text = "111111"
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if self.navigationController != nil {
            self.navigationController!.isNavigationBarHidden = true
        }
    }
    
    @IBAction func onTapBtn(_ sender: UIButton) {
        let deltaY = panelView.frame.height * 0.5
        if isCollapsed {
            UIView.animate(withDuration: 0.3, animations: {
                self.panelView.transform = CGAffineTransform(translationX: 0, y: deltaY)
                self.collapseBtn.setTitle(collapseBtnTitle.Expand.rawValue, for: .normal)
            })
        } else {
            UIView.animate(withDuration: 0.3, animations: {
                self.panelView.transform = .identity
                self.collapseBtn.setTitle(collapseBtnTitle.Up.rawValue, for: .normal)
            })
        }
        
        isCollapsed = !isCollapsed
    }
    
    @IBAction func onTapLoginBtn(_ sender: UIButton) {
        if !checkParam() {
            return
        }
        
        let username = usernameTF.text!.trimmingCharacters(in: .whitespaces)
        let password = passwordTF.text!.trimmingCharacters(in: .whitespaces)
        
        Core.Api.User.login(phone: username, password: password)
            .subscribe(
                onNext: { n in
                    let token = n.data.token
                    let user = n.data.user
                    
                    //设置存储信息
                    CoreData.setToken(value: token!)
                    CoreData.setUser(value: user!)
                    
                    DispatchQueue.main.async {
                        AppDelegate.shared().goToMainController()
                    }
                },
                onError: { error in
                    print((error as! CoreApiError).message)
                })
            .disposed(by: Core.Api.disposeBag)
    }
    
    private func checkParam() -> Bool {
        let username = usernameTF.text?.trimmingCharacters(in: .whitespaces) ?? ""
        let password = passwordTF.text?.trimmingCharacters(in: .whitespaces) ?? ""
        
        if (username == "") {
            print("username is empty")
            return false
        }
        
        if (password == "") {
            print("password is empty")
            return false
        }
        
        return true
    }
    
    @IBAction func onTapRegisterBtn(_ sender: UIButton) {
        let registerController = UIStoryboard(name: "Login", bundle: nil).instantiateViewController(withIdentifier: "RegisterController") as! RegisterController
        
        self.navigationController!.pushViewController(registerController, animated: true)
    }
}

// MARK: - UI相关操作
extension LoginController {
    override func setAdaptUI() {
        
    }
    
    func initUIData() {
        isCollapsed = true
        collapseBtn.setTitle(collapseBtnTitle.Up.rawValue, for: .normal)
    }
}

// MARK: - UITextFieldDelegate
extension LoginController: UITextFieldDelegate {
    // 设置输入框的代理
    func setTFDelegate() {
        usernameTF.delegate = self
        passwordTF.delegate = self
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        view.endEditing(true)
        return true
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
}
